using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO
{
    /// <summary>
    /// Klasa dla operacji bazodanowych specyficznych dla tabeli <code>languages</code>.
    /// </summary>
    public class LanguageDAO : Abstracts.AbstractGenericDAO<Language, System.Nullable<int>>,
        Interfaces.ILanguageDAO
    {

        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "language_id";
        }

        /// <summary>
        /// Pobieranie obiektu na podstawie unikalnego skrotu.
        /// </summary>
        /// <param name="languageAbbr">klucz glowny</param>
        /// <returns>rekord o zadanym skrocie lub null, gdy nie ma</returns>
        public Language FindByAbbr(string languageAbbr)
        {
            if (String.IsNullOrEmpty(languageAbbr))
            {
                return null;
            }   //if
            try
            {
                return this._cmsEntitiesDataContext.Languages.SingleOrDefault(
                    lang => lang.language_abbr.Equals(languageAbbr)
                );
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie listy obiektow na podstawie ich nazwy.
        /// </summary>
        /// <param name="languageName">nazwa</param>
        /// <returns>lista rekordow o zadanej nazwie</returns>
        public IQueryable<Language> FindByName(string languageName)
        {
            if (String.IsNullOrEmpty(languageName))
            {
                return null;
            }   //if
            try
            {
                IQueryable<Language> languages = this._cmsEntitiesDataContext.Languages.Where(
                    lang => lang.language_name.Equals(languageName)
                );
                if (!(languages.Any()))
                {
                    return null;
                }   //if
                return languages;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie domyslnego jezyka.
        /// </summary>
        /// <returns>domyslny jezyk</returns>
        public Language GetDefaultLanguage()
        {
            try
            {
                Language language = this._cmsEntitiesDataContext.Languages.First(
                    lang => lang.language_is_default.Equals(true)
                );
                return language;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie domyslnego jezyka.
        /// </summary>
        /// <returns>domyslny jezyk</returns>
        public Language GetDefaultLanguage(int? userId)
        {
            if (!userId.HasValue)
            {
                return null;
            }   //if
            if (userId <= 0)
            {
                return null;
            }   //if
            try
            {
                IQueryable<Language> languages = this.FindAcceptableByUserId(userId);
                if (languages == null)
                {
                    return null;
                }   //if
                if (!languages.Any())
                {
                    return null;
                }   //if
                return languages.First();
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public IQueryable<Language> FindAcceptableByUserId(int? userId)
        {
            if (!userId.HasValue)
            {
                return null;
            }   //if
            if (userId <= 0)
            {
                return null;
            }   //if
            try
            {
                return this._cmsEntitiesDataContext.Languages.Where(
                    lang => (this._cmsEntitiesDataContext.UserLanguages.Where(
                        ul => ul.user_id.Equals(userId.Value)).
                        Select(ul => ul.language_id).Contains(lang.language_id)
                    )).OrderBy(lang => lang.language_name);
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

    }
}
